package cn.com.zzz.taskpk.tcpip;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.servlet.ServletContext;

import org.apache.commons.lang.StringUtils;

import cn.bbs.ba.base.BaseDto;
import cn.bbs.ba.html.ZPaiConstant;
import cn.bbs.ba.json.JSONUntil;
import cn.bbs.ba.log.LogTrac;
import cn.bbs.ba.mi.BAMima;
import cn.bbs.ba.path.BasePath;
import cn.bbs.ba.socket.BusCodeToBusBase;
import cn.bbs.ba.util.CommonUntil;


/**
 * 监听socket服务器
 * @author xieyong
 *
 */
public class SocketThread843 extends Thread {
	
	private ServerSocket serverScoket=null;
	private ServletContext servletContext;
	
	public SocketThread843(ServerSocket serverScoket,ServletContext servletContext)
	{
		try {
			if(null==serverScoket)
			{
				this.serverScoket = new ServerSocket(843);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.servletContext=servletContext;
	}
	
	public void run()
	{
		while(!this.isInterrupted())
		{
			try {
				String xml = "<?xml version=\"1.0\"?><cross-domain-policy><site-control permitted-cross-domain-policies=\"all\"/><allow-access-from domain=\"*\" to-ports=\"*\"/></cross-domain-policy>\0";

				while(true){
					Socket socket = serverScoket.accept();
					BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
					PrintWriter pw = new PrintWriter(socket.getOutputStream());
					char[] by = new char[22];
					br.read(by, 0, 22);
					String s = new String(by);
					System.out.println("s="+s);
					if (s.equals("<policy-file-request/>")) 
					{
						 pw.print(xml);
						 pw.flush();
						 br.close();
						 pw.close();
						 socket.close();
					}
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void closeSocketServer()
	{
			try {
				if(null!=serverScoket && !serverScoket.isClosed())
				{
					serverScoket.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}